package org.sjs.dreamfly.model

import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.PrimaryKey
import androidx.room.TypeConverter
import androidx.room.TypeConverters
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import org.sjs.dreamfly.biz.movie.MovieConstants
import org.sjs.dreamfly.biz.movie.MovieUtil
import org.sjs.dreamfly.common.util.JsonUtil
import org.sjs.dreamfly.model.ixigua.ProfilePhoto

@Entity(tableName = "movie_info")
@TypeConverters(Converters::class)
data class MovieInfo (
    @PrimaryKey
    var id: String,
    var name: String?,
    val refer: String?,
    var channel: String?,
    var totalVideoCount: Int = 0,
    var imageUrl: String? = null,
    var coverList: List<Cover>? = null,
    var introduction: String? = null,
    var subTitle: String? = null,
    var actors: String? = null,
//    var actorsList: List<Actor>? = null,
    var direct: String? = null,
//    var directorList: List<Actor>? = null,
    var region: String? = null,
    var year: String? = null,
    var language: String? = null,
    var score: String? = null,
    var category: String? = null,
    var rawData: String? = null,
    var host: String? = null,
) {
    @Ignore
    var videoList = mutableListOf<VideoInfo>()
    init {
        id = MovieUtil.getMovieId(this)
        score?.split('\n')?.let {
            if (it.size > 1) {
                language = it[1]
            }
        }
    }

    fun getLandscapeImage(): String? {
        return coverList?.firstOrNull {
            it.imageStyle == Cover.LANDSCAPE
        }?.url
    }

    fun getPortraitImage(): String? {
        return coverList?.firstOrNull {
            it.imageStyle == Cover.PORTRAIT
        }?.url
    }

    fun isSingle(): Boolean? {
        return MovieUtil.isSingletonMovie(this.channel)
    }

    companion object {
        fun fromJson(json: String): MovieInfo? {
            return JsonUtil.fromJson<MovieInfo>(json)
        }
    }

    override fun toString(): String {
        return JsonUtil.toJson(this)
    }
}

data class Cover(
    var height: String?,
    var imageStyle: Int?,
    var largeUrlList: List<String?>?,
    var mediumUrlList: List<String?>?,
    var thumbUrlList: List<String?>?,
    var uri: String?,
    var url: String?,
    var urlList: List<String?>?,
    var width: String?
) {
    companion object {
        const val LANDSCAPE = 1
        const val PORTRAIT = 2
    }
}

data class Actor(
    var celebrityId: String?,
    var celebrityType: Int?,
    var name: String?,
    var profilePhotoList: List<ProfilePhoto?>?,
    var rank: Int?,
    var roleName: String?,
    var summary: String?
)

class Converters {
    @TypeConverter
    fun fromCoverList(coverList: List<Cover>?): String? {
        return Gson().toJson(coverList)
    }
    @TypeConverter
    fun toCoverList(coverListString: String?): List<Cover>? {
        val listType = object : TypeToken<List<Cover>?>() {}.type
        return Gson().fromJson(coverListString, listType)
    }
}
